/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MAPP_MODULE_
#include "mbapp.h"
#include "os_obj.h"                                          // RTOS object definitions
#include "pincfg.h"

#define TEST_MODE       MB_RTU_SLAVE

mb_baud_tab_create;
    
uint16_t DataRecevie[64];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

typedef struct
{
    uint16_t reg_r[64];
    uint16_t reg_w[64];
    uint16_t pv_r[64];
    uint16_t pv_w[64];
    
    uint8_t line_w_req;
    uint8_t line_len;
    uint16_t line[64];
    
    uint8_t coil_r[16];
    uint8_t coil_w[16];
    
    uint8_t auto_cmd_len;
    uint8_t auto_cmd[64];
    
    uint16_t page_req;
    uint16_t page_set;
    
} mb_tst_type;


mb_tst_type mb_tst;

volatile uint32_t dgs_err;
volatile uint32_t dgs_ok;

void dgus_callback(uint8_t CmdSite, uint8_t ErrSt)
{
    if(ErrSt)
    {
        dgs_err++;
    }
    else
    {
       dgs_ok++; 
    }    
}

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/   
void modbus_task (void const *argument)
{
    mb_obj_init(&mb.obj00);
    mb_obj_init(&mb.obj01);
    mb_obj_init(&mb.obj02);
    mb_obj_init(&mb.obj03);
    mb_obj_init(&mb.obj04);
    mb_obj_init(&mb.obj05);
    mb_obj_init(&mb.obj06);
    mb_obj_init(&mb.obj07);
    
    mb03_Init(TEST_MODE, mb_baud_tab[MB_BAUD_38400], MB_PAR_NONE);
    mb.obj03.SlaveId = 1;
    
    mb.obj00.os_event_send = mb_os_send; 
    mb.obj03.os_event_send = mb_os_send;
    mb.obj04.os_event_send = mb_os_send;

    mmb_stc_cmd_req(&mb.obj03,1,1,FUN_CODE_WRITE_MULTIPLE_REG,mb_tst.pv_w,0x40,8,dgus_callback);
    mmb_stc_cmd_req(&mb.obj03,2,1,FUN_CODE_READ_REG,mb_tst.pv_r,0x40,8,dgus_callback);
    mmb_stc_cmd_req(&mb.obj03,3,1,FUN_CODE_READ_REG,mb_tst.reg_r,0x10,8,dgus_callback);
    
    mmb_stc_cmd_req(&mb.obj03,4,1,FUN_CODE_W_R_MULTIPLE_REG,&mb_tst.pv_r[14],0x20,16,dgus_callback);
    mmb_stc_cmd_req(&mb.obj03,5,1,FUN_CODE_W_R_MULTIPLE_REG,&mb_tst.pv_w[16],0x22,16,dgus_callback);

    mmb_stc_cmd_req(&mb.obj03,6,1,FUN_CODE_WRITE_MULTIPLE_COIL,&mb_tst.pv_r[14],0x20,16,dgus_callback);
    mmb_stc_cmd_req(&mb.obj03,7,1,FUN_CODE_READ_REG,&mb_tst.pv_w[16],0x22,16,dgus_callback);

    for(;;){
        rt_uint32_t enent;
        
        if(mb.obj03.idle_time_cnt > 6000)
        {
            mb03_Init(TEST_MODE, mb_baud_tab[MB_BAUD_38400], MB_PAR_NONE);
            mmb_stc_cmd_req(&mb.obj03,6,1,FUN_CODE_WRITE_MULTIPLE_COIL,&mb_tst.pv_r[14],0x20,16,dgus_callback);
            mmb_stc_cmd_req(&mb.obj03,7,1,FUN_CODE_READ_REG,&mb_tst.pv_w[16],0x22,16,dgus_callback);
            mb.obj03.SlaveId = 1;
        }
        
        enent = osEventFlagsWait(os_obj.eid_mb , 0, RT_EVENT_FLAG_OR, ms_ticks(100), enent);
        
        mb_poll(&mb.obj00);
        mb_poll(&mb.obj03);
        mb_poll(&mb.obj04);
    }
}


void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt){
    if(ErrSt)
        mbTestErrCnt++; 
    else
        mbTestOkCnt++;
}

